/**
 * 
 */
package com.cnhaimei.vod.logic;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.cnhaimei.base.BaseActivity;
import com.cnhaimei.com.PopupWindowEx;
import com.cnhaimei.vod.R;
import com.cnhaimei.vod.logic.EventsManager.EventsListener;
import com.cnhaimei.vod.net.NetManager;
import com.cnhaimei.vod.net.XVod;
import com.cnhaimei.vod.net.dc.CallService;
import com.cnhaimei.vod.net.dc.Player;
import com.cnhaimei.vod.util.Const;
import com.cnhaimei.vod.util.Util;

/**
 * VOD基础Activity类
 * 
 * @author Jin001.Wu@gmail.com <br>
 * @date 2011-7-1
 * 
 */
public abstract class VODActivity extends BaseActivity implements
		EventsListener {

	protected boolean isNeting = false;
	private View playView;
	private View replayView;
	private View cutView;
	private View authorView; // 原唱
	private View accompany; // 伴唱
	private TextView selSongs;
	private TextView tip;
	private PopupWindowEx mPopupWindows;
	private View adjus_music;
	private ImageView tipImage;
	
	private boolean mShowSilentTip = false;
	
	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			onClickHome();
		}
	};
	
	@Override
	public void onContentChanged() {
		super.onContentChanged();

		setClickable(R.id.home, R.id.singed, R.id.sound, R.id.selected_sings);
		setClickable(R.id.call_service, R.id.select_authorship,
				R.id.select_accompany, R.id.replay, R.id.pause, R.id.play, R.id.cut,
				R.id.adjust_mood, R.id.adjus_music, R.id.customer, R.id.more);
		setClickable(R.id.back, R.id.search);
		setTouchListener(R.id.call_service, R.id.select_authorship,
				R.id.select_accompany, R.id.replay, R.id.play, R.id.cut,
				R.id.adjust_mood, R.id.adjus_music, R.id.customer);

		playView = findViewById(R.id.pause);
		//replayView = findViewById(R.id.replay);
		cutView = findViewById(R.id.cut);
		authorView = findViewById(R.id.select_authorship); // 原唱
		//accompany = findViewById(R.id.select_accompany); // 伴唱
		selSongs = (TextView) findViewById(R.id.selected_sings);
		adjus_music = findViewById(R.id.adjus_music);
		
		tip = (TextView) findViewById(R.id.notice);
		if (tip != null){
			tip.setText(getString(R.string.cur_room, Util.CUR_ROOM));
		}

//		LayoutInflater inflater = LayoutInflater.from(this);
//		inflater.inflate(R.layout.btn_popup, null),
		tipImage = new ImageView(this);
		tipImage.setBackgroundResource(R.drawable.adjus_music_tip);
		mPopupWindows = new PopupWindowEx(tipImage,74, 74);
		refreshPlayerByServer();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		XVod.G().registerListener(EventsManager.Event.PLYER_STATE, this);
		XVod.G().registerListener(EventsManager.Event.DISCONNECT, this);
		XVod.G().registerListener(EventsManager.Event.CONNECTING, this);
		registerReceiver(mBroadcastReceiver, new IntentFilter(Const.Recieve.ClickHome));

		if (HomeActivity.getIsReconnect() && this.getClass() != HomeActivity.class){
			finish();
			return;
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if (isFinishing()) {
			XVod.G().unRegisterListener(EventsManager.Event.PLYER_STATE, this);
			XVod.G().unRegisterListener(EventsManager.Event.DISCONNECT, this);
			XVod.G().unRegisterListener(EventsManager.Event.CONNECTING, this);
		}
		unregisterReceiver(mBroadcastReceiver);
	}
	
	
	
	protected void onClickHome() {
		finish();
	}

	//切换背景
	private void switchTabbarItem(int oldId, int newId, int resId)
	{
		View view = this.findViewById(oldId);
		view.setBackgroundResource(resId);
		
		View view2 = this.findViewById(newId);		
		view.setId(newId);
		view2.setId(oldId);
	}

	//显示/隐藏 后退按钮
	protected void showBackBtn(boolean show)
	{
		findViewById(R.id.back).setVisibility(show ? View.VISIBLE : View.INVISIBLE);
	}

	//显示/隐藏 搜索按钮
	protected void showSearchBtn(boolean show)
	{
		findViewById(R.id.search).setVisibility(show ? View.VISIBLE : View.INVISIBLE);
	}

	//显示/隐藏 iHot按钮
	protected void showIHotBtn(boolean show)
	{
		findViewById(R.id.ihot).setVisibility(show ? View.VISIBLE : View.INVISIBLE);
	}
	
	//设置标题
	protected void setTitle(String title)
	{
		((TextView)findViewById(R.id.title)).setText(title);
	}
	
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.customer:
		case R.id.search:
			// showSearchWindow();
			startActivity(Const.Action.ShowSearch);
			break;
		case R.id.back:
			finish();
			break;
		case R.id.selected_sings:
			startActivity(Const.Action.ShowSelSongs);
			break;
		case R.id.singed:
			startActivity(Const.Action.ShowSingedSongs);
			break;
		case R.id.sound: {
			startActivity(Const.Action.ShowVolume);
			// XVod.G().searchSong();//testDQControler("click sound");
		}
			break;
		case R.id.home: {
			// XVod.G().requestRecord(1, 1, 10);//DQSongs.records.getSnap()
		}
			// XVOD.G().close();
//			finish();
		sendBroadcast(new Intent(Const.Recieve.ClickHome));
			break;
		case R.id.adjust_mood:
			startActivity(Const.Action.RoomStoneSet);
			break;
		// case R.id.add:
		// startActivity(Const.Action.MusicSet);
		// break;
		case R.id.call_service:			
			callService();
			break;
		case R.id.select_authorship://选择原唱
		case R.id.select_accompany://选择伴唱
			clickAuthor();
			break;
		case R.id.adjus_music://调音
//			if (this instanceof MusicSetActivity)
//			{
//				finish();
//				return;
//			}
			startActivity(Const.Action.MusicSet);
			break;
		case R.id.play://播放
		case R.id.pause://暂停
			clickPlay();
			break;
		case R.id.replay://重新播放
			XVod.G().setPlayerState(Player.PLAYER_SATA_REPL);
			break;
		case R.id.cut://切歌
			XVod.G().setPlayerState(Player.PLAYER_SATA_NEXT);
			//Util.showMyToast(this, this.getResources().getString(R.string.cut_success), 3);
			//showMyToast(this.getResources().getString(R.string.cut_success));
			break;
		case R.id.more://更多
			//启动B
//			Intent bintent = new Intent(this, MoreSettingActivity.class);
//			startActivityForResult(bintent, Const.RequestCode.MORE_SETTING);
			startActivity(Const.Action.MoreSetting, Const.RequestCode.MORE_SETTING);
			//startActivity(Const.Action.ShowVolume);
			break;
		default:
			break;
		}
	}
	
	private void callService()	{
		if (CallService.G().hasItems()) {
			startActivity(Const.Action.ShowService, Const.RequestCode.CALL_SERVICE);
		} else {
			XVod.G().callServer(0);
			showMyToast(this.getResources().getString(R.string.call_service_success));//服务信息已发出
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == Const.RequestCode.MORE_SETTING)//更多设置
		{
			switch(resultCode)
			{
			case Const.ResultCode.RECHOOSE_ROOM://重新绑定房台
				HomeActivity.doReConnectServer();
				break;
			case Const.ResultCode.CALL_SERVICE:
				callService();
				break;
			case Const.ResultCode.SINGED:
				startActivity(Const.Action.ShowSingedSongs);
				break;
			case Const.ResultCode.CHOSE:
				startActivity(Const.Action.ShowSelSongs);
				break;
			case Const.ResultCode.MUTE:
				mShowSilentTip = true;
				XVod.G().setPlayerState(Player.PLAYER_SET_SILENT); // 请求静音/取消静音
				/*
				boolean isSilent = Player.G().getSlient() == 1 ? true : false;
				if (isSilent) {
					showMyToast(this.getResources().getString(R.string.set_silent_success));//静音
				} else {
					showMyToast(this.getResources().getString(R.string.set_sound_success));//开启声音
				}
				*/
				break;
			case Const.ResultCode.MOOD:
				startActivity(Const.Action.RoomStoneSet);
				break;
			case Const.ResultCode.LIGHT://灯光控制
				startActivity(Const.Action.LightSet);
				break;
			case Const.ResultCode.HELP:
				startActivity(Const.Action.showHelp);
				break;
			case Const.ResultCode.REPLAY://重唱
				XVod.G().setPlayerState(Player.PLAYER_SATA_REPL);
				break;
			default:
				break;
			}
		}
		else if (requestCode == Const.RequestCode.CALL_SERVICE){
			if (resultCode == Const.ResultCode.CALL_SERVICE_END) {
				showMyToast(this.getResources().getString(R.string.call_service_success));//服务信息已发出		
			}
		}
	}
	
	public void onEvent(int event, Object obj, boolean isSuc) {
		switch (event) {
		case EventsManager.Event.PLYER_STATE:
			refreshPlayerByServer();
			break;
		case EventsManager.Event.DISCONNECT: {
			if (obj != null && obj instanceof Integer) {
				int id = (Integer) obj;
				if (id == NetManager.G().getDCManangerId()) {
					finish();
				} else if (id == NetManager.G().getDQManagerId()) {
					onDQManagerDisconnect();
				}
			}
		}
			break;
		case EventsManager.Event.CONNECTING: {
			if (obj != null && obj instanceof Integer) {
				int id = (Integer) obj;
				if (id == NetManager.G().getDQManagerId()) {
					onDQManagerReconnect();
				}
			}
		}
			break;
		default:
			break;
		}
	}
	
	protected void onDQManagerDisconnect() {
		
	}
	
	protected void onDQManagerReconnect() {
		
	}

	//播放/暂停
	private void clickPlay() {

		Integer tag = (Integer) playView.getTag();
		if (tag == null) {
			tag = 0;
		}
		// int state = p.getPlayerState();
		if (tag == 0) { // 播放
			playView.setBackgroundResource(R.drawable.v8_tabbar_play);
			playView.setTag(1);
			XVod.G().setPlayerState(Player.PLAYER_SATA_PAUSE);
		} else { // 暂停 or 结束
			playView.setBackgroundResource(R.drawable.v8_tabbar_pause);
			playView.setTag(0);
			XVod.G().setPlayerState(Player.PLAYER_SATA_PLAY);
		}
	}
	
	//原唱/伴唱
	private void clickAuthor() {
		Integer tag = (Integer) authorView.getTag();
		if (tag == null) {
			tag = 0;
		}
		
		if (tag == 0) { // 原唱
			authorView.setBackgroundResource(R.drawable.v8_tabbar_yuan);
			authorView.setTag(1);
			XVod.G().setPlayerSet(Player.PLAYER_SET_TRACK, Const.TRACK.AUTHOR);
		} else { // 伴唱
			authorView.setBackgroundResource(R.drawable.v8_tabbar_ban);
			authorView.setTag(0);
			XVod.G().setPlayerSet(Player.PLAYER_SET_TRACK, Const.TRACK.ACCOMPANY);
		}
	}

	protected void refreshPlayerByServer() {
		final Player p = Player.G();
		if (p == null) {
			return;
		}
		int ps = p.getPlayerState();
		if (playView != null)
		{
			if (ps == 0) { // 播放
				playView.setBackgroundResource(R.drawable.v8_tabbar_pause);
				//playView.setSelected(true);
				playView.setTag(0);
			} else { // 暂停 or 结束
				playView.setBackgroundResource(R.drawable.v8_tabbar_play);
				//playView.setSelected(true);
				playView.setTag(1);
			}
		}

		int track = p.getTrackState();
		if (track == Const.TRACK.AUTHOR) {
			authorView.setBackgroundResource(R.drawable.v8_tabbar_yuan);
		} else {
			authorView.setBackgroundResource(R.drawable.v8_tabbar_ban);
		}

		int count = p.getPlaylistCount();
		count -= 1;
		if (count < 0) {
			count = 0;
		}
		if (selSongs != null){
			selSongs.setText(String.valueOf(count));
		}
		
		
		if (mShowSilentTip)
		{
			boolean isSilent = p.getSlient() == 1 ? true : false;
			if (isSilent) {
				showMyToast(this.getResources().getString(R.string.set_silent_success));//静音
			} else {
				showMyToast(this.getResources().getString(R.string.set_sound_success));//开启声音
			}
			
			mShowSilentTip = false;
		}
		
	}
	
	private void showBtnTip(View arch, int tipImageId, int offx, int offy) {
		if (mPopupWindows.isShowing()) {
			mPopupWindows.dismiss();
		}
		tipImage.setBackgroundResource(tipImageId);
		mPopupWindows.showAsDropDown(arch, offx, offy);
	}
	
	protected void setTouchListener(int... ids) {
		for (int i = 0; i < ids.length; i++) {
			View v = findViewById(ids[i]);
			if (v != null)
				v.setOnTouchListener(mTouchClick);
		}
	}

	//按下时显示提示信息(暂时关闭）
	private OnTouchListener mTouchClick = new OnTouchListener() {

		public boolean onTouch(View v, MotionEvent event) {
			/*
			int id = v.getId();
			int resId = R.drawable.adjus_music_tip;
			int offx = - 5;
			int offy = - 134;
			switch (id) {
			case R.id.call_service:
				resId = R.drawable.call_server_tip;
				break;
			case R.id.select_authorship:
				resId = R.drawable.select_authorship_tip;
				break;
			case R.id.select_accompany:
				resId = R.drawable.select_accompany_tip;
				break;
			case R.id.replay:
				offy +=  20;
				resId = R.drawable.replay_tip;
				break;
			case R.id.play: {
				Object o = playView.getTag();
				if (o instanceof Integer && (Integer) o == 1) {
					resId = R.drawable.play_tip;
				} else {
					resId = R.drawable.pause_tip;
				}
				offy += 10;
			}
				break;
			case R.id.cut:
				offx = 0;
				offy += 20;
				resId = R.drawable.cut_tip;
				break;
			case R.id.adjust_mood:
				resId = R.drawable.adjust_mood_tip;
				break;
			case R.id.adjus_music:
				resId = R.drawable.adjus_music_tip;
				break;
			case R.id.customer:
				resId = R.drawable.search_bt_tip;
				break;

			default:
				break;
			}
			
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				showBtnTip(v, resId, offx, offy);
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_OUTSIDE:
				mPopupWindows.dismiss(true);
				break;

			default:
				break;
			}
			*/
			return false;
		}
		
	};
}
